// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        BlueButton:cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        var self = this;
        var PreviousLocation, CurrentLocation,PreviousClicked;
        self.BlueButton.on(cc.Node.EventType.TOUCH_START, function(event){
            PreviousLocation = self.node.parent.parent.getComponent('CanvasScript').CurrentLocation;
            PreviousClicked = self.node.parent.parent.getComponent('CanvasScript').ClickedLocation;
            CurrentLocation = self.node.name[10];
            self.node.parent.parent.getComponent('CanvasScript').ClickedLocation = CurrentLocation;
            if(CurrentLocation != PreviousClicked){
                if(self.node.parent.parent.getComponent('CanvasScript').ScaleZoom){
                    self.node.parent.getChildByName('icon').scaleX = 1.4;
                    self.node.parent.getChildByName('icon').scaleY = 1.4;
                    self.node.parent.parent.getChildByName('Button'+PreviousClicked).getChildByName('icon').scaleX = 0.5;
                    self.node.parent.parent.getChildByName('Button'+PreviousClicked).getChildByName('icon').scaleY = 0.5;
                }
            }
            else
            {
                if(self.node.parent.parent.getComponent('CanvasScript').ScaleZoom){
                    self.node.parent.parent.getChildByName('Button'+PreviousClicked).getChildByName('icon').scaleX = 0.5;
                    self.node.parent.parent.getChildByName('Button'+PreviousClicked).getChildByName('icon').scaleY = 0.5;
                }
                else
                {
                    self.node.parent.parent.getChildByName('Button'+PreviousClicked).getChildByName('icon').scaleX = 1.4;
                    self.node.parent.parent.getChildByName('Button'+PreviousClicked).getChildByName('icon').scaleY = 1.4;
                }
                self.node.parent.parent.getComponent('CanvasScript').ScaleZoom = !self.node.parent.parent.getComponent('CanvasScript').ScaleZoom;
            }
            if ( !self.node.parent.parent.getChildByName('Button' + CurrentLocation).getComponent('ButtonScript').Matched){//已经匹配的位置不允许获得输入光标
                if(!self.node.parent.parent.getChildByName('Button' + PreviousLocation).getComponent('ButtonScript').Matched){
                    self.node.parent.parent.getChildByName('Button' + PreviousLocation).getComponent('ButtonScript').CurrentNumber == -1;
                    self.node.parent.parent.getChildByName('Button' + PreviousLocation).getChildByName('Label' + PreviousLocation).getComponent(cc.Label).string = '';
                }
                else
                {
                    if(CurrentLocation != PreviousLocation){//输入光标去新的位置时，旧位置上匹配成功的边界值合并到匹配值上。
                        self.node.parent.parent.getChildByName('Button' + PreviousLocation).getComponent('ButtonScript').UpperBound =
                        self.node.parent.parent.getChildByName('Button' + PreviousLocation).getComponent('ButtonScript').CurrentNumber;
                        self.node.parent.parent.getChildByName('Button' + PreviousLocation).getComponent('ButtonScript').LowerBound =
                        self.node.parent.parent.getChildByName('Button' + PreviousLocation).getComponent('ButtonScript').CurrentNumber;
                        self.node.parent.parent.getChildByName('Button' + PreviousLocation).getComponent('ButtonScript').UpperTry =
                        self.node.parent.parent.getChildByName('Button' + PreviousLocation).getComponent('ButtonScript').CurrentNumber;
                        self.node.parent.parent.getChildByName('Button' + PreviousLocation).getComponent('ButtonScript').LowerTry =
                        self.node.parent.parent.getChildByName('Button' + PreviousLocation).getComponent('ButtonScript').CurrentNumber;
                    }
                }
                self.node.parent.parent.getComponent('CanvasScript').CurrentLocation = CurrentLocation;
                self.node.parent.getChildByName('Label'+CurrentLocation).getComponent(cc.Label).string = '?';
                self.node.parent.getComponent('ButtonScript').Matched = false;
                self.node.parent.getComponent('ButtonScript').CurrentNumber = -1;
            }
        })
    },

    start () {

    },

    update (dt) {
        var self = this;
        if (!self.node.parent.getComponent('ButtonScript').KnownCondition) {
            self.node.opacity = 0;
        }
        else
        {
            self.node.opacity = 255;
        }
    },
});
